package project02.cool;

import java.util.Map;

import org.slim3.controller.validator.AbstractValidator;
import org.slim3.util.ApplicationMessage;

public class EqualIntegerValueValidator extends AbstractValidator {

    protected Object o;

    /**
     * Constructor.
     */
    public EqualIntegerValueValidator(Object o) {
        this(o, null);
    }

    /**
     * Constructor.
     *
     * @param message
     *            the error message
     */
    public EqualIntegerValueValidator(Object o, String message) {
        super(message);
        if (o == null) {
            this.o = null;
        }
        this.o = o;
    }

    @Override
    public String validate(Map<String, Object> parameters, String name) {
        Object value = parameters.get(name);
        if (o == null) {
            return null;
        }
        if (value == null || "".equals(value)) {
            return null;
        }
        try {
            int req = Integer.valueOf((String)value);
            int cmp = (Integer) o;
            if (req != cmp) {
                if (message != null) {
                    return message;
                }
                return ApplicationMessage.get("validator.EqualIntegerValue", getLabel(name));
            }
            return null;
        } catch (Throwable ignore) {
//            if (message != null) {
//                return message;
//            }
//            return ApplicationMessage.get("validator.EqualIntegerValue", getLabel(name));
            return ignore.getMessage();
        }
    }

}
